package com.shycloud.mido.music.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.music.feign.MemberFeignService;
import com.shycloud.mido.music.mapper.*;
import com.shycloud.mido.music.service.MisUserCollectionService;
import com.shycloud.mido.music.vo.CollCourseWareVO;
import com.shycloud.mido.music.vo.CourseWareMusicIPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/music/courseware")
@Api(tags = "课件选择音乐模块")
public class MusicCoursewareController {

	/** 收藏曲集 service */
	private MisUserCollectionMapper userCollectionMapper;
	/** 自定义曲集 service */
	private MisMyCollectionMapper misMyCollectionMapper;

	private MisUserCollectionService userCollectionService;

	private MemberFeignService memberFeignService;

	private MisUserMusicMapper userMusicMapper;

	private MisMusicMapper musicMapper;

	private MisMyConllectionMusicMapper conllectionMusicMapper;

	@ApiOperation(value = "获取收藏课件收藏列表", tags = "课件整合相关接口")
	@GetMapping("getSaveCollectionList")
	public R getSaveCollectionList() {

		IPage<CourseWareMusicIPageVO> res = new Page<>(1, 3);

		List<CourseWareMusicIPageVO> resList = new ArrayList<>();

		// 　＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝　收藏单曲　START＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝//
		CourseWareMusicIPageVO courseWareUserMusic = new CourseWareMusicIPageVO();
		courseWareUserMusic.setTitle("单曲");
		List<CollCourseWareVO> userMusic = new ArrayList<>();
		CollCourseWareVO collCourseWareVO = new CollCourseWareVO();
		collCourseWareVO.setId(1);
		collCourseWareVO.setName("单曲收藏");
		collCourseWareVO.setType("0");
		userMusic.add(collCourseWareVO);
		courseWareUserMusic.setCollections(userMusic);
		resList.add(courseWareUserMusic);
		// 　＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝　收藏单曲　E N D＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝//

		// 　＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝　收藏曲集　START＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝//
		String memberId = SecurityUtils.getMember().getId();
		CourseWareMusicIPageVO courseWareUserCollection = new CourseWareMusicIPageVO();
		//查询此用户收藏的曲集
		List<CollCourseWareVO> userCollection = userCollectionMapper.getUserCollectionCourseWare(memberId);
		//添加标题
		courseWareUserCollection.setTitle("曲集");
		//添加数据
		courseWareUserCollection.setCollections(userCollection);
		resList.add(courseWareUserCollection);
		// 　＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝　收藏曲集　E N D＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝//

		// 　＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝　收藏自建曲集　START＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝//
		CourseWareMusicIPageVO courseWareMyCollection = new CourseWareMusicIPageVO();
		//查询此用户收藏的自建曲集
		List<CollCourseWareVO> myCollection = misMyCollectionMapper.getMyCollectionCourseWare(memberId);
		//添加标题
		courseWareMyCollection.setTitle("自建曲集");
		//添加数据
		courseWareMyCollection.setCollections(myCollection);
		resList.add(courseWareMyCollection);
		// 　＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝　收藏自建曲集　E N D＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝//

		res.setRecords(resList);
		return new R(res);

	}

	@ApiOperation(value = "获取收藏课件课件列表", tags = "课件整合相关接口")
	@GetMapping("getCoursewareList")
	public R getCoursewareList() {

		IPage<CourseWareMusicIPageVO> res = new Page<>(1, 10000000);

		List<CourseWareMusicIPageVO> resList = new ArrayList<>();

		//先获取课件分组
		R groupList = memberFeignService.getGroupList();

		List<Map<String, Object>> feignRes = (List<Map<String, Object>>) groupList.getData();

		for (Map<String, Object> feignRe : feignRes) {
			CourseWareMusicIPageVO courseWareMusicIPageVO = new CourseWareMusicIPageVO();
			courseWareMusicIPageVO.setTitle((String) feignRe.get("name"));
			courseWareMusicIPageVO.setLibId((Integer) feignRe.get("id"));
			//获取课件文件夹列表，返回结果
			List<CollCourseWareVO> collCourseWareVOS = userCollectionService.getCoursewareFolderList((Integer) feignRe.get("id"));
			courseWareMusicIPageVO.setCollections(collCourseWareVOS);
			resList.add(courseWareMusicIPageVO);
		}
		res.setRecords(resList);
		//通过分组获取课件文件夹
		return new R(res);
	}

	@ApiOperation(value = "根据type获取详情", tags = "课件整合相关接口")
	@GetMapping("getDetailByType")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "主键ID", paramType = "query", dataType = "int", required = false),
			@ApiImplicitParam(name = "type", value = "类型 0-单曲收藏 1-曲集 2-自建曲集 3-课件", paramType = "query", dataType = "String", required = true),
			@ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int", required = true),
			@ApiImplicitParam(name = "size", value = "每页显示条数", paramType = "query", dataType = "int", required = true),
			@ApiImplicitParam(name = "teacherId", value = "教师ID", paramType = "query", dataType = "String", required = true),
			@ApiImplicitParam(name = "studentId", value = "学生ID", paramType = "query", dataType = "String", required = true)
	})
	public R getDetailByType(Integer id, String type, Integer current, Integer size, String teacherId, String studentId) {

		//获取 memberId
		String memberId = SecurityUtils.getMember().getId();

		switch (type) {
			case "0":
				//获取收藏单曲列表
				return new R(userMusicMapper.getUserMusicForCourseWare(new Page(current, size), memberId, teacherId, studentId));
			case "1":
				//获取曲集下的曲目
				return new R(musicMapper.getMusicPageForCourseWare(new Page(current, size), id, teacherId, studentId));
			case "2":
				//获取自建曲集下的曲目
				return new R(conllectionMusicMapper.getMyMusicForCourseWare(new Page(current, size),id, teacherId, studentId));
			case "3":
				//获取课件详情
				return new R(userCollectionMapper.getCoursewareForCourseWare(new Page(current, size),id, teacherId, studentId));
		}

		return new R(CommonConstants.FAIL, "type 参数有误", false);
	}

}
